package view;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.laoxinwen.app.R;

import activity.main.MainActivity;
import common.BaseActivity;

/**
 * Created by sshong on 15/6/25.
 */
public class UserGuide extends FrameLayout implements View.OnClickListener {

    private boolean ableClickRemove = false;

    public UserGuide(Context context) {
        super(context);
    }

    public UserGuide(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UserGuide(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public UserGuide(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private View getDotView() {
        return findViewById(R.id.dot);
    }

    private Runnable flashDot = new Runnable() {
        @Override
        public void run() {
            View dotView = getDotView();
//            AlphaAnimation animation = new AlphaAnimation(dotView.getAlpha(), 0.4f);
            AlphaAnimation animation = new AlphaAnimation(0.3f, 0.8f);
            animation.setDuration(300);
            animation.setRepeatCount(Animation.INFINITE);
            animation.setRepeatMode(Animation.REVERSE);
            animation.setFillBefore(true);
            animation.setFillAfter(true);
            dotView.startAnimation(animation);
        }
    };

    /**
     * 设置引导tip
     */
    public void setTip(final View toView, String tip, boolean mask, int delay) {
        this.setVisibility(INVISIBLE);

        final View maskView = findViewById(R.id.mask);
        final View dotView = findViewById(R.id.dot);
        final TextView tipTxt = (TextView) findViewById(R.id.tip);

        maskView.setVisibility(mask ? VISIBLE : GONE);
        tipTxt.setText(tip);

        this.setClickable(true);
        this.setOnClickListener(this);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //status bar height
                int sbarH = 50;
                int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    sbarH = getResources().getDimensionPixelSize(resourceId);
                }
                //测量
                int[] dotLocation = new int[2];
                toView.getLocationInWindow(dotLocation);
                int dotX = dotLocation[0] - 5;
                int dotY = dotLocation[1] - sbarH - 5;
                int dotSize = Math.min(100, Math.max(toView.getWidth(), toView.getHeight()) + 10);

                DisplayMetrics displayMetrics = new DisplayMetrics();
                ((BaseActivity) toView.getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

                int tipX = dotX;
                if (tipTxt.getWidth() + tipX > displayMetrics.widthPixels) {
                    tipX = displayMetrics.widthPixels - tipTxt.getWidth() - 10;
                }
                int tipY = dotY - tipTxt.getHeight() - 30;
                if (tipY < 0) {
                    tipY = dotY + dotSize + 30;
                }

                dotView.setX(dotX);
                dotView.setY(dotY);
                ViewGroup.LayoutParams layoutParams = dotView.getLayoutParams();
                layoutParams.width = layoutParams.height = dotSize;
                dotView.setLayoutParams(layoutParams);

                tipTxt.setX(tipX);
                tipTxt.setY(tipY);

                setVisibility(VISIBLE);

                new Handler().post(flashDot);
            }
        }, delay);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ableClickRemove = true;
            }
        }, 4000);
    }

    @Override
    public void onClick(View v) {
        if (ableClickRemove) {
            ((ViewGroup) v.getParent()).removeView(this);
            getDotView().clearAnimation();
        }
    }
}
